package com.knowif.backend.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.knowif.backend.dto.InterviewExperienceDto;
import com.knowif.pojo.*;
import com.knowif.service.InterviewExperience_service;
import com.knowif.service.OfferInterviewComment_service;
import com.knowif.service.OfferInterviewThanksCollection_service;
import com.knowif.service.User_service;
import com.knowif.system.model.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019/6/5 0005 20:58
 */
@Service
public class InterviewExperienceService {

    @Autowired
    private InterviewExperience_service interviewExperience_service;
    @Autowired
    private OfferInterviewThanksCollection_service offerInterviewThanksCollectionService;
    @Autowired
    private OfferInterviewComment_service offerInterviewCommentService;
    @Autowired
    private User_service tUserService;

    /**
     * 条件查询interviewExperience
     * @param interviewExperienceExample
     * @return
     */
    public List<InterviewExperience> selectInterviewExperience(InterviewExperienceExample interviewExperienceExample){
        return interviewExperience_service.selectInterviewExperience(interviewExperienceExample);
    }

    /**
     * 条件查询interviewExperience
     * @param interviewExperienceExample
     * @param pageable 是否需要分页
     * @return
     */
    public List<InterviewExperience> selectInterviewExperience(InterviewExperienceExample interviewExperienceExample, boolean pageable){
        if(! pageable){
            PageHelper.clearPage();
        }
        return interviewExperience_service.selectInterviewExperience(interviewExperienceExample);
    }

    /**
     * 保存interviewExperience
     * @return
     */
    public boolean saveInterviewExperience(InterviewExperience interviewExperience){
        //先删除面经
//        InterviewExperienceExample interviewExperienceExample = new InterviewExperienceExample();
//        InterviewExperienceExample.Criteria criteria = interviewExperienceExample.createCriteria();
//        criteria.andUserIdEqualTo(interviewExperience.getUserId());
//        interviewExperience_service.deleteInterviewExperienceExampleByParams(interviewExperienceExample);
        //保存面经
        int i = interviewExperience_service.saveInterviewExperience(interviewExperience);
        if(i == 0){
            return false;
        }
        return true;
    }

    /**
     * 保存interviewExperience
     * @return
     */
    public boolean updateInterviewExperienceNotNull(InterviewExperience interviewExperience){
        int i = interviewExperience_service.updateInterviewExperienceNotNull(interviewExperience);
        if(i == 0){
            return false;
        }
        return true;
    }

    /**
     * 保存interviewExperience
     * @return
     */
    public boolean updateInterviewExperience(InterviewExperience interviewExperience){
        int i = interviewExperience_service.updateInterviewExperience(interviewExperience);
        if(i == 0){
            return false;
        }
        return true;
    }

    /**
     * 删除interviewExperience
     * @return
     */
    public boolean deleteInterviewExperience(int interviewExperienceId){
        int i = interviewExperience_service.deleteInterviewExperience(interviewExperienceId);
        if(i == 0){
            return false;
        }
        return true;
    }

    /**
     * 主键查询
     * @param id
     * @return
     */
    public InterviewExperience selectById(int id){
        return interviewExperience_service.selectInterviewExperienceById(id);
    }

    public RestBean<?> detail(int interviewId, int userId) {
        InterviewExperience interviewExperience = interviewExperience_service.selectInterviewExperienceById(interviewId);
        if(interviewExperience == null){
            return RestBean.buildError("面经不存在");
        }
        InterviewExperienceDto interviewExperienceDto = new InterviewExperienceDto();
        interviewExperienceDto.setInterviewExperience(interviewExperience);
        interviewExperienceDto.setTUser(tUserService.selectById(interviewExperience.getUserId()));

        //感谢收藏类型1感谢2收藏
        OfferInterviewThanksCollectionExample thanksExample = new OfferInterviewThanksCollectionExample();
        thanksExample.createCriteria().andCollectionTypeEqualTo((byte)2)//1offer2面经
                .andThanksCollectionTypeEqualTo(1)//1感谢2收藏
                .andOfferInterviewIdEqualTo(interviewId).andUserIdEqualTo(userId);
        int thanks = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(thanksExample);

        OfferInterviewThanksCollectionExample collectionExample = new OfferInterviewThanksCollectionExample();
        collectionExample.createCriteria().andCollectionTypeEqualTo((byte)2)
                .andThanksCollectionTypeEqualTo(2)
                .andOfferInterviewIdEqualTo(interviewId).andUserIdEqualTo(userId);
        int collections = offerInterviewThanksCollectionService.countOfferInterviewThanksCollectionByParams(collectionExample);

        OfferInterviewCommentExample offerInterviewCommentExample = new OfferInterviewCommentExample();
        offerInterviewCommentExample.createCriteria()
                .andCommentTypeEqualTo(2).andParentIdIsNull().andOfferInterviewIdEqualTo(interviewId);
        offerInterviewCommentExample.setOrderByClause("comment_time DESC");
        List<OfferInterviewComment> offerInterviewComments = offerInterviewCommentService.selectOfferInterviewComment(offerInterviewCommentExample);

        JSONObject result = new JSONObject();
        result.put("interviewExperience", interviewExperienceDto);
        result.put("isThanks", thanks > 0 ? true : false);
        result.put("isCollections", collections > 0 ? true : false);
        result.put("comments", offerInterviewComments);

        return RestBean.buildSuccess(result);
    }
}
